<?php
namespace App\DBs\Stock;

use Illuminate\Support\Facades\DB;

class PlusDB extends BaseDB
{
    /**
     * 创作
     * uid 用户UID
     * vid 视频ID
     * del 0未删除，1已删除
     */

    protected $table = 'stock_plus';
    protected $tCollect = 'stock_collect'; //库收藏
    protected $tStock = 'stock'; //库

    public function __construct()
    {
    }

    /**
     * 后台列表
     */
    public function getAdminIndex($page, $size, $vid, $uid)
    {
        $pageStart = $page * $size;
        $query = DB::table($this->table)
//            ->where('del',0)
            ->orderBy('id','desc');
        if ($vid) { $query = $query->where('vid',$vid); }
        if ($uid) { $query = $query->where('uid',$uid); }
        $queryTotal = $query->count();
        $models = $query->skip($pageStart)
            ->take($size)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            //库视频信息
            $resStocks = DB::table($this->tStock)
//                ->where('del',0)
                ->where('id',$model['vid'])
                ->orderBy('id','desc')
                ->skip(0)->take(1)
                ->get();
            $stockOne = count($resStocks) ? $this->jsonToArr($resStocks[0]) : '';
            $stockTitle = $stockOne ? $stockOne['fileTitle'] : '';
            $stockImg = $stockOne ? $stockOne['fileImg'] : '';
            $stockLink = $stockOne ? $stockOne['originLink'] : '';
            $stockType = $stockOne ? $stockOne['fromType'] : '';
            //用户信息
            $resUser = $this->getUnameByUid($model['uid']);
            $dataArr[] = [
                'plusId' => $model['id'],
                'uid' => $model['uid'],
                'uname' => $resUser,
                'videoId' => $model['vid'],
                'videoTitle' => $stockTitle,
                'videoType' => $stockType,
                'videoImg' => $stockImg,
                'videoLink' => $stockLink,
                'del' => $model['del'],
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr, 'total'=>$queryTotal);
    }

    /**
     * 后台库视频
     */
    public function getAdminStocks()
    {
        $vids = array();
        $dataArr = array();
        $models = DB::table($this->table)
//            ->distinct('vid')
            ->orderBy('id','asc')
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据', 'data'=>$dataArr);
        }
        $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            if (!in_array($model['vid'],$vids)) {
                $resStocks = DB::table($this->tStock)
                    ->where('id',$model['vid'])
                    ->orderBy('id','desc')
                    ->skip(0)->take(1)
                    ->get();
                $stockOne = count($resStocks) ? $this->jsonToArr($resStocks[0]) : '';
                $stockTitle = $stockOne ? $stockOne['fileTitle'] : '';
                $dataArr[] = [
                    'videoId' => $model['vid'],
                    'videoTitle' => $stockTitle,
                ];
                $vids[] = $model['vid'];
            }
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    /**
     * 后台用户
     */
    public function getAdminUsers()
    {
        $uids = array();
        $dataArr = array();
        $models = DB::table($this->table)
//            ->distinct('uid')
            ->orderBy('id','asc')
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据', 'data'=>$dataArr);
        }
        $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            if (!in_array($model['uid'],$uids)) {
                $resUser = $this->getUnameByUid($model['uid']);
                $dataArr[] = [
                    'uid' => $model['uid'],
                    'uname' => $resUser,
                ];
                $uids[] = $model['uid'];
            }
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    /**
     * APP列表
     */
    public function getAppIndex($uid)
    {
        $query = DB::table($this->table)
            ->where('del',0)
            ->orderBy('id','desc');
        if ($uid) { $query = $query->where('uid',$uid); }
        $queryTotal = $query->count();
        $models = $query->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            //库视频信息
            $resStocks = DB::table($this->tStock)
                ->where('id',$model['vid'])
                ->orderBy('id','desc')
                ->skip(0)->take(1)
                ->get();
            $stockOne = count($resStocks) ? $this->jsonToArr($resStocks[0]) : '';
            $stockTitle = $stockOne ? $stockOne['fileTitle'] : '';
            $stockImg = $stockOne ? $stockOne['fileImg'] : '';
            $stockLink = $stockOne ? $stockOne['fileLink'] : '';
            $stockType = $stockOne ? $stockOne['fromType'] : '';
            //用户信息
            $resUser = $this->getUnameByUid($model['uid']);
            $dataArr[] = [
                'plusId' => $model['id'],
                'uid' => $model['uid'],
                'uname' => $resUser,
                'videoId' => $model['vid'],
                'videoTitle' => $stockTitle,
                'videoType' => $stockType,
                'videoImg' => $stockImg,
                'videoLink' => $stockLink,
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr, 'total'=>$queryTotal);
    }

    /**
     * 通过ID，获取一条
     */
    public function getAppOne($plusId,$uid)
    {
        $models = DB::table($this->table)
            ->where('del',0)
            ->where('id',$plusId)
            ->where('uid',$uid)
            ->orderBy('id','desc')
            ->skip(0)->take(1)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $model = $this->jsonToArr($models[0]);
        //库视频信息
        $resStocks = DB::table($this->tStock)
            ->where('id',$model['vid'])
            ->orderBy('id','desc')
            ->skip(0)->take(1)
            ->get();
        $stockOne = count($resStocks) ? $this->jsonToArr($resStocks[0]) : '';
        $stockTitle = $stockOne ? $stockOne['fileTitle'] : '';
        $stockImg = $stockOne ? $stockOne['fileImg'] : '';
        $stockLink = $stockOne ? $stockOne['fileLink'] : '';
        $stockType = $stockOne ? $stockOne['fromType'] : '';
        $ratio = 1280 / 720;
        //用户信息
        $resUser = $this->getUnameByUid($model['uid']);
        $dataArr = [
            'plusId' => $model['id'],
            'uid' => $model['uid'],
            'uname' => $resUser,
            'videoId' => $model['vid'],
            'videoTitle' => $stockTitle,
            'videoType' => $stockType,
            'videoImg' => $stockImg,
            'videoRatio' => $ratio,
            'videoLink' => $stockLink,
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    /**
     * 通过用户，获取一条
     */
    public function getAppOneByUid($uid,$vid)
    {
        $models = DB::table($this->table)
            ->where('del',0)
            ->where('vid',$vid)
            ->where('uid',$uid)
            ->orderBy('id','desc')
            ->skip(0)->take(1)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $model = $this->jsonToArr($models[0]);
        //库视频信息
        $resStocks = DB::table($this->tStock)
            ->where('id',$model['vid'])
            ->orderBy('id','desc')
            ->skip(0)->take(1)
            ->get();
        $stockOne = count($resStocks) ? $this->jsonToArr($resStocks[0]) : '';
        $stockTitle = $stockOne ? $stockOne['fileTitle'] : '';
        $stockImg = $stockOne ? $stockOne['fileImg'] : '';
        $stockLink = $stockOne ? $stockOne['fileLink'] : '';
        $stockType = $stockOne ? $stockOne['fromType'] : '';
        $ratio = 1280 / 720;
        //用户信息
        $resUser = $this->getUnameByUid($model['uid']);
        $dataArr = [
            'plusId' => $model['id'],
            'uid' => $model['uid'],
            'uname' => $resUser,
            'videoId' => $model['vid'],
            'videoTitle' => $stockTitle,
            'videoType' => $stockType,
            'videoImg' => $stockImg,
            'videoRatio' => $ratio,
            'videoLink' => $stockLink,
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    /**
     * 加入创作增加
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function setAdd($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->table)->insertGetId([
                'uid' => $uid,
                'vid' => $vid,
                'created_at' => time(),
            ]);
            $dataArr = array(
                'lastId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }
}